#! /bin/sh
#
# Copyright (C) 2016, Northwestern University and Argonne National Laboratory
# See COPYRIGHT notice in top-level directory.
#
# This forms the basis for the pnetcdf-config utility, which tells you
# various things about the PnetCDF installation. This code was based on
# file nc-config.in from netCDF distribution.
#
# $Id$
#
# @configure_input@

prefix=INSTALL_PREFIX
exec_prefix=INSTALL_EXEC_PREFIX
libdir="@libdir@"
includedir="@includedir@"

# Preprocessing:
FC_DEFINE="@FC_DEFINE@"
CPP="@CPP@"

thread_safe_mode="@ENABLE_THREAD_SAFE@"
profiling_mode="@PNETCDF_PROFILING@"
debug_mode="@PNETCDF_DEBUG@"

CPPFLAGS="@CPPFLAGS@"
if test "x$debug_mode" = x1 ; then
   if test "x$CPPFLAGS" = x ; then
      CPPFLAGS="-DPNETCDF_DEBUG"
   else
      CPPFLAGS="$CPPFLAGS -DPNETCDF_DEBUG"
   fi
fi

# Compilation:
MPICC="@MPICC@"
MPICXX="@MPICXX@"
MPIF77="@MPIF77@"
MPIF90="@MPIF90@"

# debugging and optimization options for compiling and linking
CFLAGS="@CFLAGS@"
CXXFLAGS="@CXXFLAGS@"
FFLAGS="@FFLAGS@"
FCFLAGS="@FCFLAGS@"

# Linking:
FLIBS="@FLIBS@"
FCLIBS="@FCLIBS@"
FLDFLAGS="@FLDFLAGS@"
LDFLAGS="@LDFLAGS@"
LIBS="@LIBS@"

version="@PACKAGE_NAME@ @PNETCDF_VERSION@"
CC=${MPICC}
F77=${MPIF77}
F90=${MPIF90}

has_fortran="@has_fortran@"
has_cxx="@has_mpicxx@"
enable_erange_fill="@ENABLE_ERANGE_FILL@"
enable_subfiling="@ENABLE_SUBFILING@"
enable_null_byte_header_padding="@ENABLE_NULL_BYTE_HEADER_PADDING@"
enable_relax_coord_bound="@RELAX_COORD_BOUND@"
is_bigendian="@ac_cv_c_bigendian@"
in_place_swap="@in_place_swap@"
enable_bbdriver="@ENABLE_BURST_BUFFER@"
enable_netcdf4="@ENABLE_NETCDF4@"
enable_adios="@ENABLE_ADIOS@"

if test "x$enable_netcdf4" = x1; then
   # NetCDF4 header files are required to build PnetCDF, but not PnetCDF applications
   # CPPFLAGS="$CPPFLAGS @NETCDF4_INC@"
   LDFLAGS="@NETCDF4_LDFLAGS@ $LDFLAGS"
   LIBS="@NETCDF4_LIBS@ $LIBS"
fi

if test "x$enable_adios" = x1; then
   # ADIOS header files are required to build PnetCDF, but not PnetCDF applications
   LDFLAGS="@ADIOS_LDFLAGS@ $LDFLAGS"
   LIBS="@ADIOS_LIBS@ $LIBS"
fi

config_date="@CONFIG_DATE@"
release_date="PNETCDF_RELEASE_DATE"

usage()
{
    cat <<EOF
pnetcdf-config is a utility program to display the build and installation
information of the PnetCDF library.

Usage: pnetcdf-config [OPTION]

Available values for OPTION include:

  --help                      display this help message and exit
  --all                       display all options
  --cc                        C compiler used to build PnetCDF
  --cflags                    C compiler flags used to build PnetCDF
  --cppflags                  C pre-processor flags used to build PnetCDF
  --has-c++                   whether C++ API is installed
  --c++                       C++ compiler used to build PnetCDF
  --cxxflags                  C++ compiler flags used to build PnetCDF
  --has-fortran               whether Fortran API is installed
  --f77                       Fortran 77 compiler used to build PnetCDF
  --fflags                    Fortran 77 compiler flags used to build PnetCDF
  --fppflags                  Fortran pre-processor flags used to build PnetCDF
  --fc                        Fortran 9x compiler used to build PnetCDF
  --fcflags                   Fortran 9x compiler flags used to build PnetCDF
  --ldflags                   Linker flags used to build PnetCDF
  --libs                      Extra libraries used to build PnetCDF
  --netcdf4                   Whether NetCDF-4 support is enabled or disabled
  --adios                     Whether ADIOS support is enabled or disabled
  --relax-coord-bound         Whether using a relaxed coordinate boundary check
  --in-place-swap             Whether using buffer in-place Endianness byte swap
  --erange-fill               Whether using fill values for NC_ERANGE error
  --subfiling                 Whether subfiling is enabled or disabled
  --null-byte-header-padding  Whether to check null-byte padding in header
  --burst-buffering           Whether burst buffer driver is built or not
  --profiling                 Whether internal profiling is enabled or not
  --thread-safe               Whether thread-safe capability is enabled or not
  --debug                     Whether PnetCDF is built with debug mode
  --prefix                    Installation directory
  --includedir                Installation directory containing header files
  --libdir                    Installation directory containing library files
  --version                   Library version
  --release-date              Date of PnetCDF source was released
  --config-date               Date of PnetCDF library was configured
EOF
    exit $1
}

all()
{
        echo
        echo "This $version was built with the following features:"
        echo
        echo "  --has-c++                   -> $has_cxx"
        echo "  --has-fortran               -> $has_fortran"
if test "x$enable_netcdf4" = x1; then
        echo "  --netcdf4                   -> enabled"
else
        echo "  --netcdf4                   -> disabled"
fi
if test "x$enable_adios" = x1; then
        echo "  --adios                     -> enabled"
else
        echo "  --adios                     -> disabled"
fi
if test "x$enable_relax_coord_bound" = x1; then
        echo "  --relax-coord-bound         -> enabled"
else
        echo "  --relax-coord-bound         -> disabled"
fi
if test "x$is_bigendian" = xno ; then
    if test "x$in_place_swap" = xno ; then
        echo "  --in-place-swap             -> disabled"
    elif test "x$in_place_swap" = xyes ; then
        echo "  --in-place-swap             -> enabled"
    else
        echo "  --in-place-swap             -> auto"
    fi
fi
if test "x$enable_erange_fill" = x0; then
        echo "  --erange-fill               -> disabled"
else
        echo "  --erange-fill               -> enabled"
fi
if test "x$enable_subfiling" = x1; then
        echo "  --subfiling                 -> enabled"
else
        echo "  --subfiling                 -> disabled"
fi
if test "x$enable_null_byte_header_padding" = x1; then
        echo "  --null-byte-header-padding  -> enabled"
else
        echo "  --null-byte-header-padding  -> disabled"
fi
if test "x$enable_bbdriver" = x1; then
        echo "  --burst-buffering           -> enabled"
else
        echo "  --burst-buffering           -> disabled"
fi
if test "x$profiling_mode" = x1; then
        echo "  --profiling                 -> enabled"
else
        echo "  --profiling                 -> disabled"
fi
if test "x$thread_safe_mode" = x1; then
        echo "  --thread-safe               -> enabled"
else
        echo "  --thread-safe               -> disabled"
fi
if test "x$debug_mode" = x1; then
        echo "  --debug                     -> enabled"
else
        echo "  --debug                     -> disabled"
fi
        echo
        echo "This $version was built using the following compilers and flags:"
        echo
        echo "  --cc            -> $MPICC"
if test "x$has_cxx" = xyes ; then
        echo "  --cxx           -> $MPICXX"
fi
if test "x$has_fortran" = xyes ; then
        echo "  --f77           -> $MPIF77"
        echo "  --fc            -> $MPIF90"
fi
        echo "  --cppflags      -> $CPPFLAGS"
        echo "  --cflags        -> $CFLAGS"
if test "x$has_cxx" = xyes ; then
        echo "  --cxxflags      -> $CXXFLAGS"
fi
if test "x$has_fortran" = xyes ; then
        echo "  --fflags        -> $FFLAGS"
        echo "  --fcflags       -> $FCFLAGS"
#        echo "  --fclibs                    -> $FCLIBS"
fi
        echo "  --ldflags       -> $LDFLAGS"
        echo "  --libs          -> $LIBS"
        echo
        echo "This $version has been installed under the following directories:"
        echo
        echo "  --prefix        -> $prefix"
        echo "  --includedir    -> $includedir"
        echo "  --libdir        -> $libdir"
        echo
        echo "Additional information:"
        echo
        echo "  --version       -> $version"
        echo "  --release-date  -> $release_date"
        echo "  --config-date   -> $config_date"
}

if test $# -eq 0; then
    usage 1
fi

while test $# -gt 0; do
    case "$1" in
    # this deals with options in the style
    # --option=value and extracts the value part
    # [not currently used]
    -*=*) value=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) value= ;;
    esac

    case "$1" in

    --help)
        usage 0
        ;;

    --all)
        all
        ;;

    --cc)
        echo "$MPICC" | sed -e 's/^[ \t]*//'
        ;;

    --cflags)
        echo "$CFLAGS" | sed -e 's/^[ \t]*//'
        ;;

    --cppflags)
        echo "$CPPFLAGS" | sed -e 's/^[ \t]*//'
        ;;

    --prefix)
       echo "$prefix" | sed -e 's/^[ \t]*//'
       ;;

    --includedir)
       echo "$includedir" | sed -e 's/^[ \t]*//'
       ;;

    --libdir)
       echo "$libdir" | sed -e 's/^[ \t]*//'
       ;;

    --ldflags)
       echo "$LDFLAGS" | sed -e 's/^[ \t]*//'
       ;;

    --libs)
       echo "$LIBS" | sed -e 's/^[ \t]*//'
       ;;

    --version)
        echo "$version" | sed -e 's/^[ \t]*//'
        ;;

    --has-c++)
        echo "$has_cxx" | sed -e 's/^[ \t]*//'
        ;;

    --cxx)
        echo "$MPICXX" | sed -e 's/^[ \t]*//'
        ;;

    --cxxflags)
        echo "$CXXFLAGS" | sed -e 's/^[ \t]*//'
        ;;

    --has-fortran)
        echo "$has_fortran" | sed -e 's/^[ \t]*//'
        ;;

    --f77)
        echo "$MPIF77" | sed -e 's/^[ \t]*//'
        ;;

    --fflags)
        echo "$FFLAGS" | sed -e 's/^[ \t]*//'
        ;;

    --fc)
        echo "$MPIF90" | sed -e 's/^[ \t]*//'
        ;;

    --fcflags)
        echo "$FCFLAGS" | sed -e 's/^[ \t]*//'
        ;;

#    --fclibs)
#        echo "$FCLIBS" | sed -e 's/^[ \t]*//'
#        ;;

    --netcdf4)
if test "x$enable_netcdf4" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --adios)
if test "x$enable_adios" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --relax-coord-bound)
if test "x$enable_relax_coord_bound" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --in-place-swap)
if test "x$is_bigendian" = xno ; then
   if test "x$in_place_swap" = xno ; then
        echo "disabled"
   elif test "x$in_place_swap" = xyes ; then
        echo "enabled"
   else
        echo "auto"
   fi
fi
        ;;

    --erange-fill)
if test "x${enable_erange_fill}" = xno; then
        echo "disabled"
else
        echo "enabled"
fi
        ;;

    --subfiling)
if test "x$enable_subfiling" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --null-byte-header-padding)
if test "x$enable_null_byte_header_padding" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --burst-buffering)
if test "x$enable_bbdriver" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --profiling)
if test "x$profiling_mode" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --thread-safe)
if test "x$thread_safe_mode" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --debug)
if test "x$debug_mode" = x1; then
        echo "enabled"
else
        echo "disabled"
fi
        ;;

    --release-date)
        echo "$release_date"
        ;;

    --config-date)
        echo "$config_date"
        ;;

    *)
        echo "unknown option: $1"
        usage 1
        ;;
    esac
    shift
done

exit 0
